<?php
class AdminDientoan6x36 extends CActiveRecord{
    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }

    // dat luat cho model
    public function rules(){}         

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'ketqua_dientoan6x36';
    }

    // nhan cac thuong tinh 
    public function attributeLabels(){}
    
    public function getCount($begin="",$end="") {
        $row = array();
        if($begin=="" && $end==""){
            $sql = "SELECT count(*) as count
            FROM ketqua_dientoan6x36";
        } else{
            if($begin<=$end)
            {
                $sql = "SELECT count(*) as count 
                FROM ketqua_dientoan6x36 WHERE ngay_quay >= '".$begin."' AND ngay_quay <= '".$end."'";
            } else{
                $sql = " id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user
                FROM ketqua_dientoan6x36 WHERE ngay_quay >= '".$end."' AND ngay_quay <= '".$begin."'";
            }
        }
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();

        return $row;
    }
    public function getRowByDate($date){
        // $cache = false;
        $rows = array();

        $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,ketqua_1,ketqua_2,ketqua_3,ketqua_4,ketqua_5,ketqua_6 
        FROM ketqua_dientoan6x36 WHERE ngay_quay='".$date."'";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $rows = $command->queryRow();

        return $rows;
    }   

    public function getRowInDate($first,$row_per_page,$begin="",$end=""){
        $day_now = date("Y-m-d",time()); 

        $rows = array();
        if($begin=="" && $end==""){
            $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user,modify_user 
            FROM ketqua_dientoan6x36 ORDER BY id DESC LIMIT ".$first.",".$row_per_page.""; 
        } else {
            if($begin<=$end)
            {
                $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user,modify_user 
                FROM ketqua_dientoan6x36 WHERE ngay_quay >= '".$begin."' AND ngay_quay <= '".$end."' ORDER BY id DESC LIMIT ".$first.",".$row_per_page."";
            } else{
                $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user,modify_user 
                FROM ketqua_dientoan6x36 WHERE ngay_quay >= '".$end."' AND ngay_quay <= '".$begin."' ORDER BY id DESC LIMIT ".$first.",".$row_per_page."";
            } 
        } 

        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $rows = $command->queryAll();

        return $rows;
    }
       
    public function getRowById($id){
        $row = array();
        $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,ketqua_1,ketqua_2,ketqua_3,ketqua_4,ketqua_5,ketqua_6 
        FROM ketqua_dientoan6x36 WHERE id=".$id;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();

        return $row;
    }
    public function getIdByDate($date){
        $row = array();

        $sql = "SELECT id FROM ketqua_dientoan6x36 WHERE ngay_quay='".$date."'";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();
        return $row;
    }  
    
    public function insertRow($ngay_quay,$label,$ketqua,$user){
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_dientoan6x36(ngay_quay,".$label.",create_user,create_date) 
                VALUES ('".$ngay_quay."','".$ketqua."', '".$user."', '".$date."')";    
        //echo $sql;die; 
        $command = $connect->createCommand($sql);         
        $result = $command->execute(); 
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    
    public function updateRow($id,$label,$ketqua,$user){
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_dientoan6x36 SET ".$label."='".$ketqua."',modify_user='".$user."',modify_date='".$date."'
               WHERE id =".$id; 
        //echo $sql;die;
        $command = $connect->createCommand($sql);      
        $result = $command->execute(); 
        return $result;
    }
    
    public function deleteRowById($id){
        $sql = "DELETE FROM ketqua_dientoan6x36 WHERE id=".$id;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $result = $command->execute();
        return $result;
    }
}
